Polymer({
    //id要与html的dom-module的id保持一致
    is: 'PC-CARD-HTML-4764356755066632-01',
    properties: {
        widget_title: {
            type: String,
            export: true,
            default: '待办任务'
        },
        target_platform: {
            type: String,
            export: true,
            default: 'WEB'
        },
        icon: {
            type: String
        },
        boundKeys: {
            type: Array
        },
        top5boundKeys: {
            type: Array
        },
        total: {
            type: String
        },
        noneData: {
            type: Boolean
        },
        moreData: {
            type: Boolean
        },
        taskTitle: {
            type: String
        }
    },
    addPlaceType: function(widget) {
        if (parent.window.AmpUtils.getWidgetColNumberOrAtSide(widget) == 'side') {
            widget.widgetPlaceType = 'side';
        }
    },
    stampToTime: function(stp) {
        stp = stp ? stp.toString().replace(/-/g, '/') : new Date();
        stp = parseInt(Date.parse(new Date(parseInt(stp))));
        var date = new Date(stp);
        var now = new Date();
        now.setHours(0);
        now.setMinutes(0);
        now.setSeconds(0);
        var today = now.getTime();
        var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
        var oneDayTime = 24 * 60 * 60 * 1000;

        var yesterday = '昨天';
        var todayStr = '今天';
        var month = '月';
        var date = '日';
        if (AmpGlobal.currentLang == "en_US") {
            yesterday = $.i18n("yesterday");
            todayStr = $.i18n("today");
            month = $.i18n("month");
            date = $.i18n("date");
        }

        if (stp < today) {
            // 非当日消息
            if (stp > (today - oneDayTime)) {
                return yesterday + ' ' + date.getHours() + ':' + minutes;
            } else {
                return (date.getMonth() * 1 + 1) + month + ' ' + date.getDate() + date + ' ' + date.getHours() + ':' + minutes;
            }
        } else {
            // 当日消息
            return todayStr + ' ' + date.getHours() + ':' + minutes;
        }
    },
    openProcess: function(e) {
        var $target = $(e.target);
        if ($target.closest('.view-comment').length) {
            this.viewComment($target);
        } else if ($target.closest('.check-flow').length) {
            this.viewFlowChart($target);
        } else {
            var url = $target.closest('.process-item').attr('url');
            window.open(url, '_blank');
        }
    },
    viewComment: function($target) {
        var appId = $target.attr('appId');
        var processIntanceId = $target.attr('processIntanceId');
        var url = parent.window.AMPConstant.taskCenterRequestPath + 'sys/taskCenter/*default/index.do?min=1#/flowComments/' + appId + '/' + processIntanceId;
        var checkAdvice = '查看办理意见';
        if (AmpGlobal.currentLang == "en_US") {
        	checkAdvice = $.i18n("checkAdvice");
        }
        window.parent.AmpDialog({
            title: checkAdvice,
            height: 500,
            remote: {
                url: url
            }
        });
    },
    viewFlowChart: function($target) {
        var appId = $target.attr('appId');
        var processIntanceId = $target.attr('processIntanceId');
        var url = parent.window.AMPConstant.taskCenterRequestPath + 'sys/taskCenter/*default/index.do?min=1#/flowChart/' + appId + '/' + processIntanceId;
        window.open(url, '_blank');
    },
    openApp: function() {
        window.open(parent.window.AMPConstant.taskCenterRequestPath + 'sys/taskCenter/*default/index.do#/todoTask', '_blank');
    },
    ready: function() {
        //请求数据和展现
        var widget = this;
        widget.widget_title = '待办任务';
        widget.noDataTitle = '待办任务';
        widget.seeMore = '查看更多 ';
        widget.widgetItemStr = ' 项';
        widget.noneData = true;
        if (AmpGlobal.currentLang == "en_US") {
            widget.widget_title = $.i18n("todoList");
            widget.noDataTitle = $.i18n("todoList");
            widget.seeMore = $.i18n("seeMore");
            widget.widgetItemStr = $.i18n("itemCard");
        }

        $.ajax({
            type: 'get',
            url: parent.window.AMPConstant.taskCenterRequestPath + 'sys/taskCenter/portalDataReceive/getPortalDataTask.do',
            dataType: 'jsonp',
            data: {
                'flag': 1
            },
            jsonp: 'callback',
            cache: false,
            success: function(data) {
                /**
                 * 数据说明：下面3个新字段为最新添加的，数据库中没有这几个字段
                 * 其他字段按应用管理平台数据为准
                 * 'isSingleData': true,   // true表示单个数据，false表示组合数据
                 * 'isDisplayData': false,  // 是否显示集合数据，true为显示，false为不显示
                 * 'dataNum': ',         //  表示集合数据的条数
                 **/
                data.taskData = data[0].taskData;
                data.taskDataTotal = data[0].taskDataTotal;
                if (data.taskData) {
                    data.taskData.forEach(function(item) {
                        item.taskTitle = (item.author ? item.author + '-' : '') + (item.subject || '');
                        item.toDo = "待办";
                        item.approvalAdvice = "审批意见";
                        item.flowChart = "流程图";
                        item.itemsStr = "条";
                        if (AmpGlobal.currentLang == "en_US") {
                            item.toDo = $.i18n("ToDoOnly");
                            item.flowChart = $.i18n("flowChart");
                            item.approvalAdvice = $.i18n("approvalAdvice");
                            item.itemsStr = $.i18n("items");
                        }
                    });
                    widget.boundKeys = data.taskData;
                    widget.total = data.taskDataTotal - data.taskData.length;
                    if (data.taskDataTotal == 0) {
                        widget.noneData = true;
                    } else {
                        widget.noneData = false;
                    }
                    widget.moreData = widget.total > 0;
                } else {
                    widget.boundKeys = [];
                    widget.noneData = true;
                    widget.total = '0';
                }
                widget.addPlaceType(widget);
                widget.fire('widgetComplete');
                setTimeout(function() {
                    window.parent.AmpUtils.niceScrollInit($('.PC-CARD-HTML-4764356755066632-01').find('.amp-wf-body'));
                }, 750);
            },
            error: function() {
                widget.noneData = true;
            }
        });
    }
});